package edu.hcmus.sow.web.project;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import edu.hcmus.sow.domain.Project;
import edu.hcmus.sow.domain.TaskWorkLog;
import edu.hcmus.sow.service.ProjectService;
import edu.hcmus.sow.service.TaskService;

@Controller
public class ProjectController {
   @Autowired
   private ProjectService projectService;

   @Autowired
   private TaskService taskService;

   @RequestMapping("/project")
   public ModelAndView view() {
      ModelAndView mav = new ModelAndView();
      mav.setViewName("project");
      return mav;
   }

   @RequestMapping("/report")
   public ModelAndView report() {
      ModelAndView mav = new ModelAndView();
      mav.addObject("lists", null);
      mav.addObject("total", 0);
      mav.setViewName("report");
      return mav;
   }

   // JSON get Data to drop down list project
   @RequestMapping(value = "/ajax/project/search")
   @ResponseBody
   public List<Project> getAllProject() {
      List<Project> list = projectService.findAll();
      return list;
   }

   @RequestMapping(value = "ajax/report/create", method = RequestMethod.POST)
   public ModelAndView createReport(@RequestParam("member") Integer userID, @RequestParam("project") Integer projectID,
         @RequestParam("fromDate") String fromDate, @RequestParam("toDate") String toDate) {
      ModelAndView mav = new ModelAndView();
      List<TaskWorkLog> list = taskService.getTaskWorkLogReports(userID, projectID, fromDate, toDate);
      System.out.println(projectID);
      Integer total = 0;
      if (list.size() > 0) {
         for (TaskWorkLog twl : list) {
            total += twl.getTimeSpent();
         }
      }
      mav.addObject("lists", list);
      mav.addObject("total", total);
      mav.setViewName("controls/c_timeTrackingReport");
      return mav;
   }
}
